<?php
class BaseModelSession {
    /**
     * object instance, protected static
     * @var object
     */
	protected static $instance = NULL;
	/**
	 * prefix key
	 * @var string
	 */
	const PREFIXKEY = "sess_";
	/**
	 * expired time, seconds
	 * @const uint
	 */
	const EXPIREDTIME = 1800;
	/**
	 * construnctor
	 */
    public function __construct() {
    }
    /**
     * initial session
     */
	public function __init() {	
    	if (USE_MEM_SESSION) {
    		session_write_close();
    		session_set_save_handler(array(self, "open"), 
    		                         array(self, "close"),
    		                         array(self, "read"),
    		                         array(self, "write"),
    		                         array(self, "destory"),
    		                         array(self, "gc"));
    	}
    	$_cookie = Cookie::get('PHPSESSID');
    	if (!empty($_cookie)) {
    		Cookie::set('PHPSESSID', $_cookie, "/", COOKIE_DOMAIN);
            $_id = session_id($_cookie);
        }
        @session_start();
	}
	/**
	 * single instance model
	 */
	public static function &getInstance(){
		if( !self::$instance ) {
			self::$instance = &new BaseModelSession;
			self::$instance->__init();
		}
		return self::$instance;
	}
	/**
	 * get session's name
	 * @return string
	 */
	public static function getName() {
		$_this = &BaseModelSession::getInstance();
		return session_name();
	}
	/**
	 * get session's id
	 * @return string
	 */
	public static function getSid()	{
		$_this = &BaseModelSession::getInstance();
		return session_id();
	}
	/**
	 * get session data by key
	 * @param string $key, optional
	 * @return mixed
	 */
	public static function get($key=NULL) {
		$_this = &BaseModelSession::getInstance();
		return (NULL === $key)? $_SESSION : $_SESSION[$key];
	}
	/**
	 * set session data 
	 * @param string $key
	 * @param mixed $value
	 * @return bool
	 */
	public static function set($key,$value=NULL) {
		$_this = &BaseModelSession::getInstance();
		if( NULL === $value ) {
			unset($_SESSION[$key]);
		}
		$_SESSION[$key] = $value;
		return TRUE;
	}
	/**
	 * session data truncate
	 * @return bool
	 */
	public static function destroy() {		
		$_this = &BaseModelSession::getInstance();
		self::$instance = NULL;
		return session_destroy();
	}
	/**
	 * open session handler, use mem
	 * @return bool
	 */
	public static function open($path, $name) {
		return true;
	}
	/**
	 * close session handler
	 * @return bool
	 */
	public static function close() {
		return true;
	}
	/**
	 * read session data by id
	 * @param string $sid
	 * @return string
	 */
	public static function read($sid) {
		$key = self::PREFIXKEY.$sid;
		$mc = new MCache();
		return (string)$mc->get($key);		
	}
	/**
	 * write session data into sepcial id
	 * @param string $sid
	 * @param string $data
	 */
	public static function write($sid, $data) {
		$key = self::PREFIXKEY.$sid;
		$mc = new MCache();
		return $mc->set($key, $data, self::EXPIREDTIME);
	}
	/**
	 * destory the session data by id
	 * @param string $sid
	 * @return bool
	 */
	public static function destory($sid) {
		$key = self::PREFIXKEY.$sid;
		return MCache::dD($key);		
	}
	/**
	 * garbage collector
	 * @param int $maxLifeTime
	 * @return bool
	 */
	public static function gc($maxLifeTime) {
		return true;
	}	
	/**
	 * __destruct method
	 */
	public function __destruct() {
		
	}
}
?>
